﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Setting_do_LoadEmpl : SessionPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
		string id = "0";
		if (!string.IsNullOrEmpty(Request.QueryString["id"]))
		{
			id = Request.QueryString["id"];
		}
		string dataString = ListSubDept(id);

		Response.Write(dataString);
    }

	public string ListSubDept(string parentID)
	{
		#region dynaTree
		/**
			 * res = '[' 
			 * res += '{ "title": "Node 1", "key": "k1", "isLazy": true },' 
			 * res += '{ "title": "Node 2", "key": "k2", "isLazy": true },' 
			 * res += '{ "title": "Node 3", "key": "k3", "isLazy": true }' # no trailing "," at the last line
			 * res += ']'
			 */
		try
		{
			string result = "";

			System.Text.StringBuilder sb = new System.Text.StringBuilder();

			LogicLayer mgr = new LogicLayer();

			List<SYS_Department> lstDept = mgr.ListDeptByParent(parentID);
			List<SYS_Employee> lstEmpl = mgr.ListEmployeeByDeptID(parentID);

			List<SYS_Employee> lstEmplNoDept = mgr.ListEmployeeNoDeptID();

			sb.Append("[");

			if (lstDept != null)
			{
				foreach (SYS_Department dept in lstDept)
				{
					sb.Append("{ \"title\": \"" + dept.DeptName + "\", \"key\": \"" + dept.DeptID + "\", \"isFolder\" : \"true\" ,\"isLazy\": true, \"hideCheckbox\": true ");
					sb.Append("},");
				}
			}

			if (lstEmpl != null)
			{
				foreach (SYS_Employee empl in lstEmpl)
				{
					sb.Append("{ \"title\": \"" + empl.EmplName + "\", \"key\": \"" + empl.EmplID + "\" },");
				}
			}

			if (parentID == "0")
			{
				if (lstEmplNoDept != null)
				{
					foreach (SYS_Employee empl in lstEmplNoDept)
					{
						sb.Append("{ \"title\": \"" + empl.EmplName + "\", \"key\": \"" + empl.EmplID + "\" },");
					}
				}
			}

			result = sb.ToString();
			if (result.EndsWith(","))
				result = result.Substring(0, result.Length - 1);

			result += "]";

			if (parentID == "0")
			{
				result = "[ { \"title\": \"所有部门\", \"key\": \"0\", \"isFolder\": true, \"isLazy\": true , \"expand\":true, \"hideCheckbox\": true, \"children\": " + result + "}]";				
			}

			return result;  // 输出JSON字符串
		}
		catch //(Exception ex)
		{
			//Logger.Write(ex.Message);
			return "error";
		}
		#endregion
	}
}